package com;

import com.fz.entity.Book;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.junit.Test;


import java.util.List;

/**
 * Created by webrx on 2017-09-14.
 */
public class Demo {
    @Test
    public void ddd(){
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        //NativeQuery query = session.createNativeQuery("insert into book values(null,?,?)");
        //NativeQuery query = session.createNativeQuery("insert into book(name,price) values(?,?)");
        //query.setParameter(1,"java");
        //query.setParameter(2,120);

        NativeQuery query = session.createNativeQuery("insert into book(name,price) values(:name,:price)");
        query.setParameter("price",50);
        query.setParameter("name","html5");
        query.executeUpdate();

        //Transaction tx = session.beginTransaction();
        //Query query = session.createQuery("delete from Book where id = :id");
        //query.setParameter("id",1);
        //Query query = session.createQuery("delete from Book where id = ?");
        //query.setParameter(0,1);
        //Query query = session.createQuery("delete Book where id = ?");
        //query.setParameter(0,1);
        //int i = query.executeUpdate();
        //System.out.println(i);
        //tx.commit();

        session.getTransaction().commit();
        session.beginTransaction().commit();
        session.close();
    }

    @Test
    public void ta(){
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();

        session.beginTransaction().commit();
        session.close();
    }

    @Test
    public void tt(){
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();

        //Book book = new Book();
        //book.setPrice(180);
        //book.setName("《mysql初步》");
        //session.save(book);
        //session.beginTransaction().commit();

        //List<Book> bks = session.createQuery("from Book").list();
        //List<Book> list = session.createQuery("from Book").list();
        //System.out.println(list.size());
        //for(Book b : list){
        //    System.out.println(b.getName());
        //}

        //Book book = session.get(Book.class,1);
        //book.setPrice(120);
        //book.setName("《java入门与精通》");
        //session.saveOrUpdate(book);


        //session.delete(session.load(Book.class,2));


        //Query query = session.createQuery("delete Book where id=:id");
        //query.setParameter("id",1);
        //query.executeUpdate();

        Query query = session.createQuery("from Book");
        //List<Book> b = query.list();
        //System.out.println(b.size());

        session.beginTransaction().commit();

        session.close();
    }
}
